package com.jeesite.modules.commands.web;

import com.jeesite.modules.auth.entity.AuthUser;
import com.jeesite.modules.common.Result;
import com.jeesite.modules.dto.ExecuteCommandDto;
import com.jeesite.modules.exception.CommandsException;
import com.jeesite.modules.service.UserService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.utils.ExecuteCommandsTools;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.commands.entity.Commands;
import com.jeesite.modules.commands.service.CommandsService;

import java.util.List;

/**
 * 指令表，记录指令Controller
 * @author LaVenderGong
 * @version 2025-08-20
 */
@Controller
@RequestMapping(value = "${adminPath}/commands/commands")
public class CommandsController extends BaseController {

	@Autowired
	private CommandsService commandsService;

	@Autowired
	private UserService userService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Commands get(Long tid, boolean isNewRecord) {
		return commandsService.get(tid, isNewRecord);
	}

	/**
	 * 查询列表
	 */
//	@RequiresPermissions("commands:commands:view")
	@RequestMapping(value = {"list", ""})
	public String list(Commands commands, Model model) {
		model.addAttribute("commands", commands);
		return "modules/commands/commandsList";
	}

	/**
	 * 查询列表数据
	 */
//	@RequiresPermissions("commands:commands:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Result<Page<Commands>> listData(Commands commands, HttpServletRequest request, HttpServletResponse response) {
		commands.setPage(new Page<>(request, response));
		Page<Commands> page = commandsService.findPage(commands);
		return Result.success(page);
	}

	/**
	 * 查看编辑表单
	 */
//	@RequiresPermissions("commands:commands:view")
	@RequestMapping(value = "form")
	public String form(Commands commands, Model model) {
		model.addAttribute("commands", commands);
		return "modules/commands/commandsForm";
	}

	/**
	 * 保存数据
	 */
//	@RequiresPermissions("commands:commands:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public Result<String> save(@Validated Commands commands) {
		commandsService.save(commands);
		return Result.success(renderResult(Global.TRUE, text("保存指令表，记录指令成功！")));
	}

	/**
	 * 删除数据
	 */
//	@RequiresPermissions("commands:commands:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Result<String> delete(Commands commands) {
		commandsService.delete(commands);
		return Result.success(renderResult(Global.TRUE, text("删除指令表，记录指令成功！")));
	}


	@RequestMapping("execute")
	@ResponseBody
	public Result<String> execute(@RequestBody ExecuteCommandDto executeCommandDto){
		Commands commands = commandsService.get(executeCommandDto.getCommandId());

		if(commands == null){
			throw new CommandsException("没有该命令！");
		}
		AuthUser userObj = userService.findById(Long.valueOf(executeCommandDto.getUserId()));
		String replace = commands.getCommandString().replace("@p", userObj.getNickname());
		return Result.success(ExecuteCommandsTools.execute(replace)) ;

	}

}
